package org.yuxiong.utils.proxy.server;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.RegexFilter;
import org.htmlparser.filters.StringFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;
import org.yuxiong.utils.DownloadException;
import org.yuxiong.utils.Utils;

import org.yuxiong.utils.HttpUtils;

public class ProxyServer {

	private static final String PATTERN_IP = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
	private static final Pattern PATTERN_PORT = Pattern.compile("<SCRIPT type=text/javascript>document.write\\(\":\"((\\+(\\w)){2,5})\\)</SCRIPT>");
	
	//private String z="3",m="4",k="2",l="9",d=0,x=5,i=7,w=6,q=8,b=1;
	
	private static List<Server> getIP(String url, String charset) throws DownloadException, ParserException{
		List<Server> ipList = null;
		
		String content = HttpUtils.callURL(url, charset);

		Parser pageParser = Parser.createParser(content, charset);
		NodeFilter filter = new RegexFilter(PATTERN_IP);
		NodeList nodelist = pageParser.extractAllNodesThatMatch(filter);
		if(null!=nodelist && nodelist.size()>0){
			ipList = new ArrayList();
			for (SimpleNodeIterator i = nodelist.elements();i.hasMoreNodes();){
				Node node = i.nextNode();
				String port = node.getNextSibling().toHtml();
				
				ipList.add(new Server(node.toHtml(), getPort(port)));
			}
		}
		return ipList;
	}
	
	public static List<String> getIP() throws DownloadException, ParserException{
		String url = null;
		List<String> ipList = new ArrayList();
		for(int i=1;i<=10;i++){
			url = "http://www.cnproxy.com/proxy"+i+".html";
			List list = getIP(url, "gb2312");
			if(list!=null)
				ipList.addAll(list);
		}
		return ipList;
	}
	
	private static int getPort(String str){
		Matcher m = PATTERN_PORT.matcher(str);
		m.find();
		String[] arr = m.group(1).split("\\+");
		String port = "";
		//z="3",m="4",k="2",l="9",d=0,x=5,i=7,w=6,q=8,b=1;
		for(int i=0;i<arr.length;i++) {
			String tmp = arr[i];
			if(tmp.equals("z")) port += "3";
			else if(tmp.equals("m")) port += "4";
			else if(tmp.equals("k")) port += "2";
			else if(tmp.equals("l")) port += "9";
			else if(tmp.equals("d")) port += "0";
			else if(tmp.equals("x")) port += "5";
			else if(tmp.equals("i")) port += "7";
			else if(tmp.equals("w")) port += "6";
			else if(tmp.equals("q")) port += "8";
			else if(tmp.equals("b")) port += "1";
		}
		return Integer.parseInt(port);
	}
	
	
	
	public static void main(String[] args){
		
		try {
			List list = getIP();
			Utils.debug(list);
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DownloadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*
	 	String regex1="<SCRIPT type=text/javascript>document.write\\(\":\"\\+(\\w)\\+(\\w)(\\+(\\w)\\+(\\w))?\\)</SCRIPT>";
        String str1="<SCRIPT type=text/javascript>document.write(\":\"+q+d)</SCRIPT>";
        Pattern p1=Pattern.compile(regex1);
        Matcher matcher=p1.matcher(str1);
        matcher.find();
		for(int i=0;i<=matcher.groupCount();i++){
			System.out.println(matcher.group(i));
		}*/
	}
	
}
